package com.sunday.android.fangdaifu.bukong.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.activity.index.IndexItemActivity;
import com.sunday.android.fangdaifu.bukong.entity.IndexItem;
import com.sunday.android.fangdaifu.bukong.entity.IndexProject;
import com.sunday.android.fangdaifu.bukong.entity.IndexTop;
import com.sunday.android.fangdaifu.bukong.utils.noticeview.NoticeShowUtils;
import com.sunday.android.fangdaifu.bukong.utils.noticeview.NoticeView;
import com.sunday.android.fangdaifu.bukong.utils.pie.HollowPieNewChart;
import com.sunday.android.fangdaifu.bukong.utils.pie.PieData;
import com.sunday.android.fangdaifu.bukong.utils.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;
import com.sunday.common.utils.PixUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class IndexWithHeadersAdapter extends RecyclerView.Adapter implements StickyRecyclerHeadersAdapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private IndexTop indexTop;//首页头部报表和新闻列表
    private List<IndexProject> projectList = new ArrayList<>();//首页项目列表'
    private int[] mColors = {0xFF59c2ff,0xff40acff,0xff2692ff};
    private String[] pieType = {"倾斜", "沉降", "裂缝(位移)"};

    public IndexWithHeadersAdapter(Context mContext,IndexTop indexTop,List<IndexProject> projectList) {
        this.mContext = mContext;
        this.indexTop = indexTop;
        this.projectList = projectList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.index_view_top, parent, false);
            return new TopHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.index_view_item, parent, false);
            return new ItemHolder(view);
        }
    }

    //单个内容
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            TopHolder itemHolder = (TopHolder) holder;
            if (indexTop != null) {
                List<PieData> pieDatas = new ArrayList<>();
                pieDatas.add(new PieData(indexTop.getSpCount(), mColors[0], pieType[0]));
                pieDatas.add(new PieData(indexTop.getCjCount(), mColors[1], pieType[1]));
                pieDatas.add(new PieData(indexTop.getLfCount(), mColors[2], pieType[2]));
                itemHolder.pie.setDataList(pieDatas);
                itemHolder.pie.setmOnCenter(new HollowPieNewChart.OnCenterDraw() {
                    @Override
                    public void draw(Canvas canvas, Paint paint) {
                        paint.setColor(0xff333333);
                        paint.setTextSize(50);
                        String total = String.valueOf(indexTop.getTotal());
                        canvas.drawText(total, -paint.measureText(total) / 2, -PixUtils.dip2px(mContext, 4), paint);
                        paint.setColor(0xff808080);
                        paint.setTextSize(26);
                        String extraTotal = "总设备数";
                        canvas.drawText(extraTotal, -paint.measureText(extraTotal) / 2, PixUtils.dip2px(mContext, 12), paint);
                    }
                });
                itemHolder.tvTotalCount.setText(String.format("%d", indexTop.getProjectCount()));
                itemHolder.tvBreakCount.setText(String.format("%d", indexTop.getSpoiled()));
                new NoticeShowUtils(itemHolder.publicNoticeView, indexTop.getList());
            }
        } else {
            ItemHolder itemHolder = (ItemHolder) holder;
            final IndexProject indexProject = projectList.get(position - 1);
            itemHolder.tvName.setText(indexProject.getName());
            itemHolder.tvDate.setText(String.format("%s 至 %s", indexProject.getStartTime(), indexProject.getEndTime()));
            itemHolder.tvAddress.setText(indexProject.getHouseInfo());
            itemHolder.tvEquipment.setText(String.format("倾斜 %d   沉降 %d   裂缝(位移) %d",indexProject.getHorizontal(),indexProject.getSettlement(),indexProject.getCrack()));
            itemHolder.lMain.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, IndexItemActivity.class);
                    intent.putExtra("id",indexProject.getId());
                    intent.putExtra("title",indexProject.getName());
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getHeaderId(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.index_view_header, parent, false);
        return new HeaderHolder(view);
    }

    //头部内容
    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
        HeaderHolder itemHolder = (HeaderHolder) holder;
        if (position!=0){
            IndexProject indexProject = projectList.get(position-1);
            if (indexProject.isHaveHeader()) {
                itemHolder.tvHeader.setVisibility(View.VISIBLE);
                itemHolder.tvHeader.setText(indexProject.getTime());
            } else {
                itemHolder.tvHeader.setVisibility(View.GONE);
            }
        }else {
            itemHolder.tvHeader.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return projectList.size()+1;
    }

    public void refresh(IndexTop indexTop){
        this.indexTop = indexTop;
        this.notifyDataSetChanged();
    }

    //top view
    class TopHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.pie)
        HollowPieNewChart pie;
        @Bind(R.id.tvTotalCount)
        TextView tvTotalCount;
        @Bind(R.id.tvBreakCount)
        TextView tvBreakCount;
        @Bind(R.id.publicNoticeView)
        NoticeView publicNoticeView;

        public TopHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //item view
    class ItemHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tvName)
        TextView tvName;
        @Bind(R.id.lMain)
        View lMain;
        @Bind(R.id.tvDate)
        TextView tvDate;
        @Bind(R.id.tvAddress)
        TextView tvAddress;
        @Bind(R.id.tvEquipment)
        TextView tvEquipment;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //header view
    class HeaderHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tvHeader)
        TextView tvHeader;

        public HeaderHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}